package com.wholesale;

import java.util.ArrayList;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.wholesale.beans.GoodsInfo;
import com.wholesale.datas.WebServiceClient;
import com.wholesale.utils.GoodsListAdapter;

public class Under3Layout extends LinearLayout implements OnClickListener {
	private ViewFlipper viewFlipper;
	private Context context;
	private View baseView;
	private ListView bodyList;
	private Button leftButton;
	private Button rightButton;
	private TextView headView;
	private Button lowToHighButton;
	private Button highToLowButton;
	private RelativeLayout loadingLayout ;

	private GoodsListAdapter listAdapter;
	private GoodsListAsync goodsListAsync;
	 private Drawable lowToHighon,lowToHighoff,highToLowoff,highToLowon;

	// 构造函数
	public Under3Layout(Context context, final ViewFlipper viewFlipper) {
		super(context);
		this.viewFlipper = viewFlipper;
		this.context = context;
		// 加载需要的属性，加载方法一的子Layout
		baseView = ((Activity) getContext()).getLayoutInflater().inflate(
				R.layout.under3, this);
		bodyList = (ListView) baseView.findViewById(R.id.bodyList);
		leftButton = (Button) baseView.findViewById(R.id.leftBtn);
		leftButton.setVisibility(View.INVISIBLE);
		rightButton = (Button) baseView.findViewById(R.id.rightBtn);
		rightButton.setVisibility(View.INVISIBLE);
		headView = (TextView) baseView.findViewById(R.id.title);
		headView.setText("Under $3");
		loadingLayout = (RelativeLayout)baseView.findViewById(R.id.loadingLayout);
		
		bodyList.setSelector(R.drawable.details_list_bg);
		// item 单击事件
		bodyList.setOnItemClickListener(moreClieckListener);
		listAdapter = new GoodsListAdapter(context, bodyList);
		listAdapter.setAsyncLoadData(true);
		bodyList.setAdapter(listAdapter);

		lowToHighButton = (Button) baseView
				.findViewById(R.id.under_low_to_high);
		lowToHighButton.setOnClickListener(this);
		highToLowButton = (Button) baseView
				.findViewById(R.id.under_high_to_low);
		highToLowButton.setOnClickListener(this);
		
		lowToHighon=this.getResources().getDrawable(R.drawable.under_segment_left_on);
		lowToHighoff=this.getResources().getDrawable(R.drawable.under_segment_left_off);
		highToLowoff=this.getResources().getDrawable(R.drawable.under_segment_right_off);
		highToLowon=this.getResources().getDrawable(R.drawable.under_segment_right_on);
		goodsListAsync = new GoodsListAsync(context);
		goodsListAsync.execute(false);
	}

	// ListView 点击事件
	OnItemClickListener moreClieckListener = new OnItemClickListener() {
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			GoodsDetailLayout goodsDetailLayout = new GoodsDetailLayout(
					context, viewFlipper,"Back", listAdapter.getItem(arg2));
			viewFlipper.addView(goodsDetailLayout);
			viewFlipper.showNext();
		}
	};

	class GoodsListAsync extends
			AsyncTask<Boolean, Integer, ArrayList<GoodsInfo>> {
		ProgressDialog pdialog;
		long goodsCount = 1;
		boolean desc;

		public GoodsListAsync(Context context) {
		}

		@Override
		protected void onPreExecute() {
			listAdapter.stop();
			listAdapter.clear();
			listAdapter.notifyDataSetChanged();
			loadingLayout.setVisibility(View.VISIBLE);
		}
		
		@Override
		protected ArrayList<GoodsInfo> doInBackground(Boolean... params) {
			desc = params[0];
			goodsCount = WebServiceClient.getUnder3GoodsPageCount();
			if (goodsCount == 0) {
				return new ArrayList<GoodsInfo>();
			} else {
				return WebServiceClient.getUnder3Goods(1, "shopPrice", desc);
			}
		}

		@Override
		protected void onPostExecute(ArrayList<GoodsInfo> result) {
			listAdapter.clear();
			listAdapter.addItems(result);
			listAdapter.notifyDataSetChanged();
			listAdapter.setPageNum(GoodsListAdapter.ACTION_UNDERLIST, (int) goodsCount, "shopPrice", desc);
//			pdialog.dismiss();
			loadingLayout.setVisibility(View.GONE);
		}

		@Override
		protected void onCancelled() {
			super.onCancelled();
		}
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.leftBtn:
			viewFlipper.showPrevious();
			viewFlipper.removeView(this);
			break;
		case R.id.under_low_to_high:
			lowToHighButton.setBackgroundDrawable(lowToHighon);
			highToLowButton.setBackgroundDrawable(highToLowoff);
			if(goodsListAsync!=null){
				goodsListAsync.cancel(true);
			}
			goodsListAsync = new GoodsListAsync(context);
			goodsListAsync.execute(false);
			break;

		case R.id.under_high_to_low:
			lowToHighButton.setBackgroundDrawable(lowToHighoff);
			highToLowButton.setBackgroundDrawable(highToLowon);
			if(goodsListAsync!=null){
				goodsListAsync.cancel(true);
			}
			goodsListAsync = new GoodsListAsync(context);
			goodsListAsync.execute(true);
			break;
		default:
			break;
		}

	}
}
